package com.corpgovernment.organization.mq;

import com.corpgovernment.common.mq.consumer.AbstractMultiTenantRocketMQConsumer;
import com.corpgovernment.common.mq.enums.MessageBizTypeEnum;
import com.corpgovernment.organization.service.budget.AlarmBudgetService;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class MultiTenantBudgetNotifyConsumer extends AbstractMultiTenantRocketMQConsumer {

    @Autowired
    private AlarmBudgetService alarmBudgetService;

    @Override
    protected void consume(MessageExt messageExt, ConsumeConcurrentlyContext context) {
        String msg = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        log.info("接收到的消息为：{}，MsgId：{}", msg, messageExt.getMsgId());
        if (StringUtils.isBlank(msg)){
            log.error("预算管控-阀值告警消费数据为空");
        }
        onBudgetNotifyMsg(msg);
    }

    /**
     * 消费预算阀值告警消息
     */
    private void onBudgetNotifyMsg(String budgetId) {
        log.info("消费预算阀值告警消息{}", budgetId);
        alarmBudgetService.sendAlarmMsg(budgetId);
    }

    @Override
    protected MessageBizTypeEnum getMessageBizType() {
        return MessageBizTypeEnum.BUDGET_NOTIFY;
    }

}
